package Servlet;

import Dao.checkDao;
import Dao.check_recordDao;
import bean.ReservationObject;
import bean.check;
import bean.check_record;
import utils.Age;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/checkServlet")
public class checkServlet extends BaseServlet{
    checkDao dao = new checkDao();
    check_recordDao recordDao = new check_recordDao();

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<check> checkList = dao.queryChecks();
        req.setAttribute("checkList",checkList);
        req.getRequestDispatcher("/").forward(req,resp);
    }
    protected void queryCheckByOffice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String department = req.getParameter("department");
        List<check> checkByOffice = dao.queryCheckByOffice(department);
        req.setAttribute("checksByOffice",checkByOffice);
        req.getRequestDispatcher("/choose_Check.jsp").forward(req,resp);
    }
    protected void queryCheckByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        check check = dao.queryCheckByName("");
        req.setAttribute("check",check);
        req.getRequestDispatcher("/").forward(req,resp);

    }
    protected void getInformation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String phonenuber = req.getParameter("phonenuber");
        String idNumber = req.getParameter("IdNumber");
        String department = req.getParameter("department");
        String time = req.getParameter("time");
        System.out.println(time);
        int age = Integer.parseInt(req.getParameter("age"));
        ReservationObject object = new ReservationObject(name,phonenuber,idNumber,sex,null,age);
        HttpSession session = req.getSession();
        session.setAttribute("patientInformation",object);

        req.getRequestDispatcher("/checkServlet?action=queryCheckByOffice").forward(req,resp);
    }

    protected void insertRecord(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        check check = dao.queryCheckById(cid);
        HttpSession session = req.getSession();
        String[] dateAndTime = Age.getDateAndTime();
        String date = dateAndTime[0];
        String time = dateAndTime[1];
        ReservationObject patientInformation = (ReservationObject) session.getAttribute("patientInformation");
        check_record record = new check_record(null,date,time,patientInformation.getName(),patientInformation.getSex(),String.valueOf(patientInformation.getAge()),check.getName(),check.getDoctor(),"Pending",null,patientInformation.getIdNumber(),null,check);
        recordDao.addRecord(record);
        req.getRequestDispatcher("/checkQueueServlet?action=checkQueue").forward(req,resp);
    }

}
